package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

//    @PostMapping("/add")
//    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
//        log.info("ShoppingCart:{}",shoppingCart);
//        //获取当前登录用户的id
//        Long id = BaseContext.getId();
//        shoppingCart.setUserId(id);
//
//        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        //判断当前加入购物车的是菜品还是套餐,并设置相应的sql语句
//        if (shoppingCart.getDishId() != null) {
//            //菜品
//            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//        } else {
//            //套餐
//            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        }
//
//        //查询shoppingCart表中是否含有当前登录用户的菜品、套餐信息 -- 当前登录用户的购物车是否含有该彩品（或 套餐）
//        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
//        // SELECT id,name,user_id,dish_id,setmeal_id,dish_flavor,number,amount,image,create_time FROM shopping_cart WHERE (setmeal_id = ? AND user_id = ?)
//        ShoppingCart shoppingCartDB = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
//
//        if(shoppingCartDB != null) {
//            //当前登录用户购物车中含有待加入的菜品（套餐）
//            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
//            // UPDATE shopping_cart SET name=?, user_id=?, setmeal_id=?, number=?, amount=?, image=?, create_time=? WHERE id=?
//            shoppingCartService.updateById(shoppingCartDB);
//        } else {
//            //当前登录用户购物车中不含有待加入的菜品（套餐）
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            //在购物车数据库中插入菜品（套餐）数据 -- shopping—_cart --> insert
//            // INSERT INTO shopping_cart ( id, name, user_id, setmeal_id, number, amount, image, create_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
//            shoppingCartService.save(shoppingCart);
//
//            shoppingCartDB = shoppingCart;
//        }
//
//        return R.success(shoppingCartDB);
//    }

    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        //获取当前登录用户id
        Long id = BaseContext.getId();
        shoppingCart.setUserId(id);

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        //待加入购物车的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        } else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前登录用户的购物车中之前是否已加入过待加入的菜品或套餐
        ShoppingCart shoppingCartDB = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);

        if (shoppingCartDB != null) {
            //之前已经加入过
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            //更新
            shoppingCartService.updateById(shoppingCartDB);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartDB = shoppingCart;
        }

        return R.success(shoppingCartDB);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getId());
        log.info("shoppingCart:{}", shoppingCart);

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != shoppingCart.getUserId(), ShoppingCart::getUserId, shoppingCart.getUserId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        return R.success(shoppingCartService.list(queryWrapper));
    }

    @DeleteMapping("clean")
    public R clean() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getId());

        shoppingCartService.remove(wrapper);

        return R.success("清空成功");
    }


}
